﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using System.Windows.Forms;
using System.IO;
using System.Collections.Specialized;
using System.Collections;

namespace CnGine.CodeGenerator.Help
{
    /// <summary>
    /// 代码生成的辅助类
    /// </summary>
    public class CodeGenerator
    {
        public static StringCollection CreateCsCode(string SaveDieratory, string templateFile, List<Models.Table> tables, ArrayList tablesName, string replaceStr)
        {
            StringBuilder sbLog = new StringBuilder();
            DateTime tb = System.DateTime.Now;
            //判断目录是否存在,不存在建立目录
            var dir = Path.GetDirectoryName(SaveDieratory);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            //筛选出要生成代码的表
            List<Models.Table> tbGenerator = new List<Models.Table>();
            foreach (string item in tablesName)
            {
                Models.Table t = tables.Find(delegate(Models.Table tt) { return tt.Name == item; });
                tbGenerator.Add(t);
            }
            sbLog.AppendFormat("===============准备生成{0}个文件===============\n", tbGenerator.Count);
            StringCollection colls = new StringCollection();
            //循环表生成代码
            for (int i = 0; i < tbGenerator.Count; i++)
            {
                Models.Table t = tbGenerator[i];
                string fileName = t.Name;
                //去除前缀
                if (!string.IsNullOrEmpty(replaceStr))
                {
                    if (fileName.IndexOf(replaceStr) > -1)
                    {
                        int index = replaceStr.Length;
                        fileName = fileName.Substring(index, fileName.Length - index);
                    }
                }
                t.Name = fileName;
                string filePath = SaveDieratory + "\\" + fileName + ".cs";
                VelocityHelper vh = new VelocityHelper();
                vh.Set("table", t);
                bool flag = vh.GeneratorCode(templateFile, filePath);
                if (flag)
                {
                    colls.Add(filePath);
                    sbLog.AppendFormat("已生成文件{0}\n", filePath);
                }
            }
            //生成好代码之后添加到项目中
            SelectedItem selectItem = Help.DTEHelper.GetSelectedItem();
            Help.DTEHelper.AddFileToProject(selectItem, colls);

            DateTime te = System.DateTime.Now;
            TimeSpan ts = te - tb;
            sbLog.AppendFormat("共生成的文件:{0}个，耗时{1}秒。\n", colls.Count, GetSeconds(ts.Milliseconds));
            Help.DTEHelper.OutPutMessage(sbLog.ToString());
            return colls;
        }
        public static string GetSeconds(int Milliseconds)
        {
            //return Milliseconds.ToString();
            decimal second = Convert.ToDecimal(Milliseconds) / 1000;
            return second.ToString("0.00");
        }
    }
}
